package ie.dkit.semanticPhotoWeb.client.panels.profile;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.dtos.PersonDTO;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.VType;

public class EditUserProfileWindow extends Window {

	protected ClientServerConnectorAsync m_ClientServerConnectorAsync;

	private FormPanel formPanel;

	private TextField username;

	private TextField name;

	private TextField email;

	private TextField homepage;

	private TextField nick;

	private TextField photo;

	private InterestPanel interestsPanel;

	private KnowsPanel knowsPanel;

	public EditUserProfileWindow(
			ClientServerConnectorAsync clientServerConnectorAsync) {
		super("Edit User Profile", true, true);
		setAutoScroll(true);
		m_ClientServerConnectorAsync = clientServerConnectorAsync;
		// Fix due issues..
		setSize(280, 430);
		formPanel = new FormPanel();
		formPanel.setMonitorValid(true);
		formPanel.add(username = new TextField("Username"));
		username.setDisabled(true);
		formPanel.add(name = new TextField("Name"));
		name.setAllowBlank(false);
		formPanel.add(email = new TextField("Email"));
		email.setVtype(VType.EMAIL);
		formPanel.add(homepage = new TextField("Homepage"));
		homepage.setVtype(VType.URL);
		formPanel.add(nick = new TextField("Nick"));
		formPanel.add(photo = new TextField("Photo"));
		photo.setVtype(VType.URL);
		formPanel.add(interestsPanel = new InterestPanel(this));
		interestsPanel.setCollapsible(true);
		formPanel.add(knowsPanel = new KnowsPanel(this));
		knowsPanel.setCollapsible(true);
		formPanel.add(new Button("Save Changes", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				MessageBox.wait("Updating profile");
				m_ClientServerConnectorAsync.updateUserProfile(new PersonDTO(
						username.getText(), name.getText(), email.getText(),
						homepage.getText(), nick.getText(), photo.getText()),
						new AsyncCallbackAdapter() {
							public void onSuccess(Object result) {
								MessageBox.alert("Succefully Changed");
							}
						});
			}
		}));
		formPanel.add(new Button("Close", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				EditUserProfileWindow.this.close();
			}
		}));
		// last action
		m_ClientServerConnectorAsync.getUserProfile(new AsyncCallbackAdapter() {

			public void onSuccess(Object result) {
				PersonDTO personDTO = (PersonDTO) result;
				username.setValue(personDTO.username);
				name.setValue(personDTO.name);
				email.setValue(personDTO.email);
				homepage.setValue(personDTO.homepage);
				nick.setValue(personDTO.nick);
				photo.setValue(personDTO.photo);
				interestsPanel.addInterests(personDTO.interests);
				knowsPanel.addPersons(personDTO.knows);
				doLayout();
			}
		});
		add(formPanel);
	}
}
